﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Web;

namespace TireService.Web.UserControls.StatusOverview
{
    public partial class ucServiceCapacityOverview : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                TireServiceDB db = new TireServiceDB();

                // Bind dropdownlist Container
                ddlContainer.DataSource = db.GetContainers();
                ddlContainer.ValueField = "ContainerId";
                ddlContainer.TextField = "ContainerNumber";
                ddlContainer.DataBind();
                ddlContainer.SelectedIndex = 1;

                BindServiceCapacityData(db, Convert.ToInt32(ddlFilterMethod.SelectedItem.Value), null);
            }
        }

        /// <summary>
        /// Bind chart data
        /// </summary>
        /// <param name="db">Connection to database</param>
        /// <param name="FilterMethod">Determin what chart to show</param>
        private void BindServiceCapacityData(TireServiceDB db, int FilterMethod, int? ContainerId)
        {
            if (FilterMethod == 0)
            {
                var servicecapacity = from p in db.ServiceCarScheduleDetails
                                      join y in db.ContainerServiceCarCoverages on p.ServiceCarId equals y.ServiceCarId into py
                                      from z in py.DefaultIfEmpty()
                                      join a in db.Containers on z.ContainerId equals a.ContainerId into na
                                      from c in na.DefaultIfEmpty()
                                      join v in db.ScheduleDetailTypes on p.ScheduleDetailTypeId equals v.ScheduleDetailTypeId into pv
                                      from x in pv.DefaultIfEmpty()
                                      where p.ScheduleDate >= DateTime.Now
                                      group c by new
                                      {
                                          c.ContainerNumber,
                                          ScheduleDetailTypeDescription = x.Description
                                      }
                                          into grp
                                          select new
                                          {
                                              grp.Key.ContainerNumber,
                                              grp.Key.ScheduleDetailTypeDescription,
                                              ScheduleDetailTypeCount = grp.Count()
                                          };

                //var containers = (from c in servicecapacity
                //                 select new { c.ContainerNumber }).Distinct();

                //foreach (var container in containers)
                //{
                //    // Create an empty Bar series and add it to the chart.
                //    Series series = new Series(container.ContainerNumber, ViewType.Bar);
                //    chartServiceCapacity.Series.Add(series);

                //    // Generate a data table and bind the series to it.
                //    series.DataSource = servicecapacity.Where(p => p.ContainerNumber == container.ContainerNumber);

                //    // Specify data members to bind the series.
                //    series.ArgumentScaleType = ScaleType.Numerical;
                //    series.ArgumentDataMember = "ScheduleDetailTypeDescription";
                //    series.ValueScaleType = ScaleType.Numerical;
                //    series.ValueDataMembers.AddRange(new string[] { "ScheduleDetailTypeCount" });
                //}

                chartServiceCapacity.SeriesDataMember = "ContainerNumber";
                chartServiceCapacity.SeriesTemplate.ArgumentDataMember = "ScheduleDetailTypeDescription";
                chartServiceCapacity.SeriesTemplate.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.Qualitative;
                chartServiceCapacity.SeriesTemplate.ValueScaleType = DevExpress.XtraCharts.ScaleType.Numerical;
                chartServiceCapacity.SeriesTemplate.ValueDataMembers[0] = "ScheduleDetailTypeCount";

                chartServiceCapacity.DataSource = servicecapacity;
                chartServiceCapacity.DataBind();
            }

            if (FilterMethod == 1)
            {
                var servicecapacity = from p in db.ServiceCarScheduleDetails
                                      join y in db.ContainerServiceCarCoverages on p.ServiceCarId equals y.ServiceCarId into py
                                      from z in py.DefaultIfEmpty()
                                      join a in db.Containers on z.ContainerId equals a.ContainerId into na
                                      from c in na.DefaultIfEmpty()
                                      join v in db.ScheduleDetailTypes on p.ScheduleDetailTypeId equals v.ScheduleDetailTypeId into pv
                                      from x in pv.DefaultIfEmpty()
                                      join o in db.ServiceCars on p.ServiceCarId equals o.ServiceCarId into po
                                      from u in po.DefaultIfEmpty()
                                      where p.ScheduleDate >= DateTime.Now & c.ContainerId == Convert.ToInt32(ContainerId)
                                      group c by new
                                      {
                                          ServiceCarDescription = u.Description,
                                          ScheduleDetailTypeDescription = x.Description
                                      }
                                          into grp
                                          select new
                                          {
                                              grp.Key.ServiceCarDescription,
                                              grp.Key.ScheduleDetailTypeDescription,
                                              ScheduleDetailTypeCount = grp.Count()
                                          };

                chartServiceCapacity.SeriesDataMember = "ServiceCarDescription";
                chartServiceCapacity.SeriesTemplate.ArgumentDataMember = "ScheduleDetailTypeDescription";
                chartServiceCapacity.SeriesTemplate.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.Qualitative;
                chartServiceCapacity.SeriesTemplate.ValueScaleType = DevExpress.XtraCharts.ScaleType.Numerical;
                chartServiceCapacity.SeriesTemplate.ValueDataMembers[0] = "ScheduleDetailTypeCount";

                chartServiceCapacity.DataSource = servicecapacity;
                chartServiceCapacity.DataBind();
            }
        }

        /// <summary>
        /// Callback function that refreshes Chart
        /// </summary>
        protected void chartServiceCapacity_CustomCallback(object sender, CustomCallbackEventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Get values (0: Method, 1: ContainerId)
            string[] param = e.Parameter.Split('|');

            int Method = Convert.ToInt32(param[0]);
            int? ContainerId = null;
            if (param[1] != null & param[1] != "null" & param[1] != string.Empty)
            {
                ContainerId = Convert.ToInt32(param[1]);
            }

            BindServiceCapacityData(db, Method, ContainerId);
        }
    }
}